/*
 * @页面名称:窗体模型验证规则解读类文件
 * @描述:
 * @作者: 研发中心
 * @Date: 2023-08-04
 */

export class FormRuler {
  formRuleList: any;
  constructor(formRuleList: any) {
    this.formRuleList = formRuleList;
  }
  getValidation() {
    try {
      const tempObj: any = {};
      this.formRuleList.forEach((item: any) => {
        if (!tempObj[item.V_TableField]) {
          tempObj[item.V_TableField] = [];
        }
        if (item.V_Method === "Is_Null") {
          // 是否为空判断
          tempObj[item.V_TableField].push({ required: true, message: item.V_Error, trigger: item.V_ControlType });
        }
        if (item.V_Method === "Is_Number") {
          // 判断是否为number类型
          tempObj[item.V_TableField].push({
            type: "number",
            message: item.V_Error,
            trigger: item.V_ControlType,
            transform: (value: any) => {
              return Number(value);
            }
          });
        }
        if (item.V_Method === "Is_Email") {
          // 判断是否为email类型
          tempObj[item.V_TableField].push({ type: "email", message: item.V_Error, trigger: item.V_ControlType });
        }
        if (item.V_Method === "Is_Section_String") {
          // 判断字符串长度类型输入区间
          const result = item.V_ValidValue.split(",");
          tempObj[item.V_TableField].push({
            min: result ? Number(result[0]) : 0,
            max: result ? Number(result[1]) : 100,
            message: item.V_Error,
            trigger: item.V_ControlType,
            transform: (value: any) => {
              return String(value);
            }
          });
        }
        if (item.V_Method === "Is_Section_Number") {
          // 判断数字输入区间
          if (item.V_ValidValue) {
            const result = item.V_ValidValue.split(",");
            tempObj[item.V_TableField].push({
              validator: (rule: any, value: any, callback: any) => {
                if (!Number.isInteger(value)) {
                  callback(new Error("非数字类型!"));
                } else {
                  if (value < Number(result[0])) {
                    callback(new Error(item.V_Error));
                  } else if (value > Number(result[1])) {
                    callback(new Error(item.V_Error));
                  } else {
                    callback();
                  }
                }
              },
              trigger: item.V_ControlType,
              transform: (value: any) => {
                return Number(value);
              }
            });
          }
        }
        if (item.V_Method === "Is_Phone") {
          // 判断输入手机号
          const rule = /^1\d{10}$/;
          tempObj[item.V_TableField].push({ pattern: rule, message: item.V_Error, trigger: item.V_ControlType });
        }
        if (item.V_Method === "Is_IDnumber") {
          // 判断身份证号
          const rule =
            /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/;
          tempObj[item.V_TableField].push({ pattern: rule, message: item.V_Error, trigger: item.V_ControlType });
        }
      });
      return tempObj;
    } catch (err) {
      console.log(err);
    }
  }
}
